package app

import (
	"APT/internal/consts"
	"APT/internal/dao"
	"APT/internal/global"
	"APT/internal/library/contexts"
	"context"
	"github.com/gogf/gf/v2/frame/g"

	"APT/api/app/member"
)

func (c *ControllerMember) BalanceChange(ctx context.Context, req *member.BalanceChangeReq) (res *member.BalanceChangeRes, err error) {
	var (
		MemberInfo = contexts.GetMemberUser(ctx)
	)
	res = new(member.BalanceChangeRes)
	orm := dao.PmsBalanceChange.Ctx(ctx).Where(dao.PmsBalanceChange.Columns().MemberId, MemberInfo.PmsMember.Id)
	if !g.IsEmpty(req.ChangeMode) {
		switch req.ChangeMode {
		case "in":
			orm = orm.WhereGT(dao.PmsBalanceChange.Columns().ChangePrice, 0)
		case "out":
			orm = orm.WhereLT(dao.PmsBalanceChange.Columns().ChangePrice, 0)
		}
	}
	if err = orm.Page(req.PageNum, req.PageSize).OrderDesc(dao.PmsBalanceChange.Columns().Id).ScanAndCount(&res.List, &res.Count, false); err != nil {
		return
	}
	for k, v := range res.List {
		if !g.IsEmpty(v.Des) && contexts.GetLanguage(ctx) != consts.Zh {
			res.List[k].Des = global.I18nMap[v.Des][contexts.GetLanguage(ctx)]
		}
	}
	return
}
